package com.helloniu.fl.util {
	import com.gnetis.tang.common.Event;
	import com.gnetis.tang.whiteboard.component.WBCanvas;
	import com.gnetis.tang.whiteboard.component.WBTool;
	import com.gnetis.tang.whiteboard.event.CreateShapeEvent;
	import com.gnetis.tang.whiteboard.shape.CompositeStyle;
	import com.gnetis.tang.whiteboard.shape.EllipseStyle;
	import com.gnetis.tang.whiteboard.shape.RectangleStyle;
	import com.gnetis.tang.whiteboard.shape.Dot;
	import com.gnetis.tang.whiteboard.shape.DotSet;
	import com.gnetis.tang.whiteboard.shape.Line;
	import com.gnetis.tang.whiteboard.shape.LineStyle;
	import com.gnetis.tang.whiteboard.shape.DotStyle;
	import com.gnetis.tang.whiteboard.shape.Rectangle;
	import com.gnetis.tang.whiteboard.shape.Ellipse;
	import com.gnetis.tang.whiteboard.shape.Shape;
	import com.gnetis.tang.whiteboard.shape.ShapeStyle;
	
	import flash.geom.Point;
	
	public class EventUtil
	{
		public function EventUtil()
		{
		}
		
		public static function generateEvent(currCanvas:WBCanvas):Event {
			var points:Array = currCanvas.getShapePoints();
			var tooltype:Number = currCanvas.getToolType();
			var toolstyle:Object = currCanvas.getToolStyle();
			
			var shape:Shape = null;
			var shapestyle:ShapeStyle = null;
			
			if(tooltype == WBTool.TOOL_LINE) {
				shape = new Line(points[0].x, points[0].y, points[points.length - 1].x, points[points.length - 1].y);
				shapestyle = new LineStyle(1, toolstyle.linewidth, toolstyle.linecolor);				
			}
			else if(tooltype == WBTool.TOOL_PEN) {
				var dots:Array = toDots(points);
				shape = new DotSet(dots);
				shapestyle = new DotStyle(1, toolstyle.linewidth, toolstyle.linecolor);				
			}
			else if(tooltype == WBTool.TOOL_OVAL) {
				// use * to avoid class name conflict
				var rect:* = WBTool.getRectangle(new Array(points[0], points[points.length - 1]));
				shape = new Ellipse(rect.x, rect.y, rect.width, rect.height);
				var linestyle:LineStyle = new LineStyle(1, toolstyle.linewidth, toolstyle.linecolor);				

				shapestyle = new EllipseStyle(linestyle, toolstyle.fillcolor);				
			}
			else if(tooltype == WBTool.TOOL_RECT) {
				rect = WBTool.getRectangle(new Array(points[0], points[points.length - 1]));
				shape = new Rectangle(rect.x, rect.y, rect.width, rect.height);
				linestyle = new LineStyle(1, toolstyle.linewidth, toolstyle.linecolor);				

				shapestyle = new RectangleStyle(linestyle, toolstyle.fillcolor);
			}
			

			shape.setStyle(shapestyle);
			var event:CreateShapeEvent = new CreateShapeEvent(shape);			

			return event;
		}
		
		private static function toDots(points:Array):Array {
			var dots:Array = new Array();
			
			for(var i:int = 0; i < points.length; i++) {
				dots.push(Dot.toDot(points[i] as Point));
			}
			
			return dots;
		}
	}
}